package com.dqx.opengl_es_demo.model;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * 渲染控件
 */
public class ModelGLSurfaceView extends GLSurfaceView {
    ModelRenderer renderer;

    public ModelGLSurfaceView(Context context) {
        this(context, null);
    }

    public ModelGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //创建OpenGL-ES 2.0版本的上下文
        setEGLContextClientVersion(2);
        //初始化渲染器
        renderer = new ModelRenderer(this);
        //设置渲染器
        setRenderer(renderer);
        //设置渲染模式为不断渲染
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    float lastX;
    float lastY;
    float baseValue;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getRawX();
        float y = event.getRawY();
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            baseValue = 0;
            lastX = event.getRawX();
            lastY = event.getRawY();
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (event.getPointerCount() == 2) {//2个手指在屏幕上

                /*********************缩放操作**********************/
                float dx = event.getX(0) - event.getX(1);
                float dy = event.getY(0) - event.getY(1);
                float value = (float) Math.sqrt(dx * dx + dy * dy);// 计算两点的距离
                if (baseValue == 0) {
                    baseValue = value;
                } else {
                    if (value - baseValue >= 10 || value - baseValue <= -10) {
                        // 当前两点间的距离除以手指落下时两点间的距离就是需要缩放的比例。
                        float scale = value / baseValue;
                        if (isCanScal()) {
                            renderer.setScale(scale);
                            requestRender();
                        }
                    }
                }
            } else if (event.getPointerCount() == 1) {//1个手指在屏幕上

                /*********************平移操作**********************/
                if (isCanTranslate()) {
                    x -= lastX;
                    y -= lastY;
                    renderer.setTranslateX(x * 0.01f);
                    renderer.setTranslateY(-y * 0.01f);
                    requestRender();
                }

                /*********************旋转操作**********************/
                if (isCanRotate()) {
                    float dy = event.getRawY() - lastY;//计算触控笔Y位移
                    float dx = event.getRawX() - lastX;//计算触控笔X位移
                    //角度缩放比例
                    float TOUCH_SCALE_FACTOR = 180.0f / 320;
                    if (Math.abs(dx) > 0 && Math.abs(dx) > Math.abs(dy)) {//水平旋转（绕Y轴旋转）
                        //旋转角度
                        float angleY = dx * TOUCH_SCALE_FACTOR;

                        renderer.setAngleY(angleY);
                        requestRender();//重绘

                    } else if (Math.abs(dy) > 0 && Math.abs(dy) > Math.abs(dx)) {//垂直旋转（绕X轴旋转）
                        //旋转角度
                        float angleX = dy * TOUCH_SCALE_FACTOR;

                        renderer.setAngleX(angleX);
                        requestRender();//重绘

                    }
                }

                lastX = event.getRawX();
                lastY = event.getRawY();

            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            renderer.setAngleX(0);
            renderer.setAngleY(0);
            renderer.setTranslateY(0);
            renderer.setTranslateX(0);
            renderer.setScale(0);
        }
        return true;
    }


    //是否允许手势缩放操作
    private boolean canScal;
    //是否允许手势平移操作
    private boolean canTranslate;
    //是否允许手势旋转操作
    private boolean canRotate;

    public boolean isCanScal() {
        return canScal;
    }

    public void setCanScal(boolean canScal) {
        this.canScal = canScal;
    }

    public boolean isCanTranslate() {
        return canTranslate;
    }

    public void setCanTranslate(boolean canTranslate) {
        this.canTranslate = canTranslate;
    }

    public boolean isCanRotate() {
        return canRotate;
    }

    public void setCanRotate(boolean canRotate) {
        this.canRotate = canRotate;
    }

}
